package com.linked.goods.goodsbase.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.goods.goodsbase.bean.dto.BaseGoodsDTO;
import com.linked.goods.goodsbase.bean.dto.GoodsAllInfoDTO;
import com.linked.goods.goodsbase.bean.po.GoodsInfoPO;
import com.linked.goods.goodsbase.bean.po.GoodsVersionPO;
import com.linked.goods.goodsbase.service.IGoodsImagesService;
import com.linked.goods.goodsbase.service.IGoodsService;
import com.linked.goods.goodsbase.service.IGoodsVersionService;
import com.linked.goods.goodsbase.service.IGoodsWareHouseService;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author :dbq
 * @date : 2023/1/28 9:38
 * @description : desc
 */
@RestController
@RequestMapping("goodshandle")
@Slf4j
public class GoodsHandleController {

    private final IGoodsService goodsService;

    private final IGoodsImagesService goodsImagesService;

    private final TransactionTemplate transactionTemplate;

    private final ObjectMapper mapper;

    private final IGoodsVersionService goodsVersionService;

    private final IGoodsWareHouseService goodsWareHouseService;

    @Autowired
    public GoodsHandleController(IGoodsService goodsService,
                                 IGoodsImagesService goodsImagesService,
                                 TransactionTemplate transactionTemplate,
                                 ObjectMapper mapper,
                                 IGoodsVersionService goodsVersionService,
                                 IGoodsWareHouseService goodsWareHouseService) {
        this.goodsService = goodsService;
        this.goodsImagesService = goodsImagesService;
        this.transactionTemplate = transactionTemplate;
        this.goodsVersionService = goodsVersionService;
        this.mapper = mapper;
        this.goodsWareHouseService = goodsWareHouseService;
    }

    @PostMapping("saveBaseGoods")
    Result saveBaseGoods(@RequestBody BaseGoodsDTO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("保存基础商品接口 入参：{}", mapper.writeValueAsString(param));
        }

        boolean ret = false;
        try {
            ret = goodsService.saveGoodsBaseInfo(new GoodsInfoPO(param));
        } catch (Exception e) {
            log.error("保存基础商品接口 异常！", e);
            return Result.error("未知异常，请联系管理员!");
        }
        return !ret ? Result.ok(false, "保存失败！") : Result.success();
    }

    @PostMapping("saveGoodsVersion")
    Result saveGoodsVersion(@RequestBody GoodsVersionPO param)throws Exception{
        if (log.isInfoEnabled()) {
            log.info("保存商品版本接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = goodsVersionService.saveGoodsVersion(param);
        } catch (Exception e) {
            log.error("保存商品版本接口 异常！", e);
            return Result.error("未知异常，请联系管理员!");
        }
        return !ret ? Result.ok(false, "保存失败！") : Result.success();
    }

}
